<?php
class Silk_Mapi_Helper_Data extends Mage_Core_Helper_Abstract
{	
	private $_redis = null;

	public function getRedis()
	{
		if($this->_redis == null)
		{
			try
			{
				$this->_redis = new Credis_Client('127.0.0.1');
			}
			catch(Exception $e)
			{
				Mage::log("connect redis fail!", null, "redis.log");
			}
		}
		return $this->_redis;
	}

	public function getenv($name, $default = null)
	{
		if($default === null)
			return getenv($name);
		else
		{
			$value = getenv($name);
			if($value)
				return $value;
			else
				return $default;
		}
	}

	public function getGiftsByProduct($product)
	{
		$_imageHelper = Mage::helper('catalog/image');
		$collection = Mage::getModel('pms/gift')->getCollection()
			->addFieldToFilter('product_id', array('eq'=>$product->getId()))
			->addValidateFilter();

		$gifts = array();
		foreach($collection as $gift)
		{
			$gifts[] = array(
				'id'=>$gift->getId(),
				'tip'=>$gift->getTip(),
				'gift_name'=>$gift->getGiftProduct()->getName()?:'',
				'gift_product_id'=>$gift->getGiftProductId(),
				'gift_image'=>$_imageHelper->init($gift->getGiftProduct(), 'image')->resize(250) . "",
				'url'=>$gift->getUrl()?:'',
				'description'=>$gift->getDescription()
			);
		}
		return $gifts;
	}

	public function getAppType()
	{
		return array(
			'101' => 'Android',
			'201' => 'Android'
		);
	}
	
}
	 